﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KendoMvcUI.Core;
using KendoMvcUI.Common;
using System.Web.Mvc;

namespace KendoMvcUI.UI
{
    public class ColorPicker:KendoUIBase
    {
        public ColorPicker(ColorPickerSettings settings,
            ViewContext viewContext,IViewDataContainer viewDataContainer)
            :base(settings,viewContext,viewDataContainer)
        { }
        public override string RenderHtml()
        {
            return "<input id=\"{0}\" type=\"color\"/>".Formator(Settings.Id);
        }
    }
    public class ColorPickerSettings : KendoUIControlSettings
    {
        public ColorPickerSettings()
            : base("kendoColorPicker")
        { }
        public bool? Buttons { get; set; }
        public int? Columns { get; set; }
        public int? TileSize { get; set; }
        [PropertySetting(IsIngoreForJavaScript = true)]
        public Action<ColorMessage> Message { get; set; }
        public string Palette { get; set; }
        public string Value { get; set; }
        #region 事件
        [PropertySetting(IsIngoreForJavaScript=true)]
        public string OnChange { get; set; }
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnSelect { get; set; }
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnOpen { get; set; }
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnClose { get; set; }
        #endregion

        public override string ToJsSettings()
        {
            string jsScript = base.ToJsSettings();
            if (!jsScript.EndsWith(","))
                jsScript += ",";
            if(Message!=null)
            {
                var msg = new ColorMessage();
                Message(msg);
                jsScript += jsScript+",";
            }

            if (!OnChange.IsNullOrWhiteSpace())
                jsScript += "change:{0},".Formator(OnChange);
            if (!OnClose.IsNullOrWhiteSpace())
                jsScript += "close:{0},".Formator(OnClose);
            if (!OnOpen.IsNullOrWhiteSpace())
                jsScript += "open:{0},".Formator(OnOpen);
            if (!OnSelect.IsNullOrWhiteSpace())
                jsScript += "select:{0},".Formator(OnSelect);

            return jsScript.TrimEnd(',');
        }
    }
    public class ColorMessage
    {
        public string Apply { get; set; }
        public string Cancel { get; set; }

        public override string ToString()
        {
            var script = "";
            if (!Apply.IsNullOrWhiteSpace())
                script += "apply:'{0}',".Formator(Apply);
            if (!Cancel.IsNullOrWhiteSpace())
                script += "cancel:'{0}',".Formator(Cancel);
            return script.TrimEnd(',');
        }
    }
}
